package com.lvmama.rhino.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lvmama.rhino.pojo.ManGodCount;
import com.lvmama.rhino.pojo.ManGodHistory;
import com.lvmama.rhino.service.ManGodHistoryService;
import com.lvmama.rhino.service.StreamService;

@RequestMapping("manGod")
@Controller
public class ManGodController {

	
	private StreamService streamService;
	@Autowired
	public void setStreamService(StreamService streamService) {
		this.streamService = streamService;
	}
	@Autowired
	public void setManGodHistoryService(ManGodHistoryService manGodHistoryService) {
		this.manGodHistoryService = manGodHistoryService;
	}

	private ManGodHistoryService  manGodHistoryService;
	
	@RequestMapping("/ajaxManGodData")
	@ResponseBody
	public Map<Integer, Map<Integer, Integer>> ajaxManGodData(){
		Map<Integer,Map<Integer,Integer>> map=new HashMap<>();
		Map<Integer,Integer> map0=new HashMap<>();
		Map<Integer,Integer> map1=new HashMap<>();
		Map<Integer,Integer> map2=new HashMap<>();
		Map<Integer,Integer> map3=new HashMap<>();
		Map<Integer,Integer> map4=new HashMap<>();
		Map<Integer,Integer> map5=new HashMap<>();
		Map<Integer,Integer> map6=new HashMap<>();
	List<ManGodCount> queryCountByHour = streamService.getManGodCountRealTime();
		/*List<ManGodCount> queryCountByHour=new ArrayList<ManGodCount>();
		ManGodCount m1=new ManGodCount();
		m1.setBarragePage(10);
		m1.setGetHb(10);
		m1.setHbActivityPage(10);
		m1.setMyHb(10);
		m1.setObtainAuthcode(10);
		ManGodCount m2=new ManGodCount();
		m2.setBarragePage(20);
		m2.setGetHb(10);
		m2.setHbActivityPage(20);
		m2.setMyHb(20);
		m2.setObtainAuthcode(20);
		ManGodCount m3=new ManGodCount();
		m3.setBarragePage(30);
		m3.setGetHb(30);
		m3.setHbActivityPage(30);
		m3.setMyHb(30);
		m3.setObtainAuthcode(30);
		queryCountByHour.add(m1);
		queryCountByHour.add(m2);
		queryCountByHour.add(m3);*/
		if(queryCountByHour==null || queryCountByHour.size()==0){
			return null;
		}
		
		if(queryCountByHour.size()<60){
			for(int i=0;i<60-queryCountByHour.size();i++){
				map0.put(i, 0);
				map1.put(i, 0);
				map2.put(i, 0);
				map3.put(i, 0);
				map4.put(i, 0);
				map6.put(i, 0);
			}
		}
		for(int i=59;i>=60-queryCountByHour.size();i--)
		{
			ManGodCount manGodCount = queryCountByHour.get(59-i);
			map0.put(i, manGodCount.getObtainAuthcode());
			map1.put(i, manGodCount.getGetHb());
			map2.put(i, manGodCount.getHbActivityPage());
			map3.put(i, manGodCount.getBarragePage());
			map4.put(i, manGodCount.getMyHb());
			map6.put(i, manGodCount.getSendTaunt());
		}
		
		map5.put(0, queryCountByHour.size());
		map.put(0, map0);
		map.put(1, map1);
		map.put(2, map2);
		map.put(3, map3);
		map.put(4, map4);
		map.put(5, map5);
		map.put(6,map6);
		return map;
	}
	
	@RequestMapping("/ajaxHistoryData")
	@ResponseBody
	public Map<Integer, Map<Integer, Integer>> ajaxHistoryData(
			@RequestParam("dateTime") String dateTime) {

		DateTimeFormatter formater = DateTimeFormat.forPattern("yyyy-MM-dd");
		Date datetime = DateTime.parse(dateTime, formater).toDate();

		Map<Integer, Map<Integer, Integer>> map = new HashMap<>();
		Map<Integer, Integer> map0 = new HashMap<>();
		Map<Integer, Integer> map1 = new HashMap<>();
		Map<Integer, Integer> map2 = new HashMap<>();
		Map<Integer, Integer> map3 = new HashMap<>();
		Map<Integer, Integer> map4 = new HashMap<>();
		Map<Integer, Integer> map5 = new HashMap<>();
		Map<Integer, Integer> map6 = new HashMap<>();
		Map<Integer, Integer> map7 = new HashMap<>();
		List<ManGodHistory> list = manGodHistoryService
				.queryHistoryData(datetime);

		if (list == null || list.size() == 0) {
			return null;
		}
		for (int i = 0; i < list.size(); i++) {
			ManGodHistory manGodHistory = list.get(i);
			map0.put(i, manGodHistory.getObtainAuthcode());
			map1.put(i, manGodHistory.getGetHb());
			map2.put(i, manGodHistory.getHbActivityPage());
			map3.put(i, manGodHistory.getBarragePage());
			map4.put(i, manGodHistory.getMyHb());
			map6.put(i, manGodHistory.getHour());
			map7.put(i, manGodHistory.getSendTaunt());
		}
		map5.put(0, list.size());
		map.put(0, map0);
		map.put(1, map1);
		map.put(2, map2);
		map.put(3, map3);
		map.put(4, map4);
		map.put(5, map5);
		map.put(6, map6);
		map.put(7, map7);
		return map;
	}
	
	@RequestMapping(value = "ajaxByDate")
	@ResponseBody
	public ManGodCount ajaxByDate(@RequestParam("startTime") String startTime,
			@RequestParam("endTime") String endTime) {
		DateTimeFormatter formater = DateTimeFormat
				.forPattern("yyyy-MM-dd HH:mm:ss");
		Date sTime = DateTime.parse(startTime, formater).toDate();
		Date eTime = DateTime.parse(endTime, formater).toDate();
		 ManGodCount queryCountByDate = manGodHistoryService.queryCountByDate(sTime, eTime);
		 return queryCountByDate;

	}
	
}
